package com.cjm.one.model.order.dto;

import lombok.Data;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.Optional;

@Data
public class OrderSummary {
    private BigDecimal totalAmount;      // 总金额
    private Integer orderCount;          // 订单总数
    private BigDecimal refundAmount;     // 退款总额
    private Integer refundOrderCount;    // 退款订单数

    private BigDecimal totalAmountGrowthRate; // 总金额同比增长率
    private BigDecimal orderCountGrowthRate; // 订单总数同比增长率
    private BigDecimal refundAmountGrowthRate; // 退款总额同比增长率
    private BigDecimal refundOrderCountGrowthRate; // 退款订单数同比增长率

    // 同比计算方法
    public void calculateGrowthRate(BigDecimal currentYear, BigDecimal lastYear) {
        if (lastYear.compareTo(BigDecimal.ZERO) == 0) {
            this.totalAmountGrowthRate = BigDecimal.ZERO;
        } else {
            this.totalAmountGrowthRate = currentYear.subtract(lastYear).divide(lastYear, 4, RoundingMode.HALF_UP);
        }
    }


    // OrderSummary.java 增加构造方法
    public OrderSummary(BigDecimal totalAmount, Integer orderCount,
                        BigDecimal refundAmount, Integer refundOrderCount) {
        this.totalAmount = Optional.ofNullable(totalAmount).orElse(BigDecimal.ZERO);
        this.orderCount = Optional.ofNullable(orderCount).orElse(0);
        this.refundAmount = Optional.ofNullable(refundAmount).orElse(BigDecimal.ZERO);
        this.refundOrderCount = Optional.ofNullable(refundOrderCount).orElse(0);
    }

    // 当查询结果为空时防止NPE
    public OrderSummary() {
        this(BigDecimal.ZERO, 0, BigDecimal.ZERO, 0);
    }

}